package com.example.icampusevent;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.icampusevent.jsonobjects.FriendEvent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.ListFragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class NotCheckedinFriendListFragment extends ListFragment {
	
	private ArrayList<FriendEvent> friendList;
	private int mode;
	
	private final int ALL = 0;
	private final int NOTCHECKEDIN = 1;
	private final int CHECKEDIN = 2;
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateFriendList();
	}
	
	
	public void updateFriendList() {
		LoadNotCheckinFriendTask task = new LoadNotCheckinFriendTask();
        long eventId = ((CheckinActivity)getActivity()).getEventId();
        String userId = ((CheckinActivity)getActivity()).getUserId();
        String url = "http://hamedaan.usc.edu:8080/team15/InviteServlet?q=allfdnc&eventid=" 
                     + eventId + "&userid=" + userId;
        task.execute(url);
	}
	
	public ArrayList<FriendEvent> getSelectedFriends() {
		ArrayList<FriendEvent> selected = new ArrayList<FriendEvent>();
		ArrayList<CheckBox> boxes = ((FriendListAdapter)getListAdapter()).getBoxes();
		for(int i = 0; i < friendList.size(); i++) {
			if(boxes.get(i).isChecked()) {
				selected.add(friendList.get(i));
			}
		}
		return selected;
	}

	private class LoadNotCheckinFriendTask extends AsyncTask<String, Integer, Reader> {
	    protected Reader doInBackground(String... url) {
	    	InputStream source = retrieveStream(url[0]);
	 		if(source == null) return null;
	 		 
	 		Reader inputReader = new InputStreamReader(source);
	 		return inputReader;
	    }

	    protected void onPostExecute(Reader json) {
	    	Type listType = new TypeToken<ArrayList<FriendEvent>>(){}.getType();
	 		Gson gson = new Gson();
			friendList = gson.fromJson(json, listType);
			setListAdapter(new FriendListAdapter(getActivity(), NOTCHECKEDIN, friendList));
	    }
	     
	    // Get data stream from server
	 	private InputStream retrieveStream(String url) {
	 		DefaultHttpClient client = new DefaultHttpClient();
	 		HttpGet request = new HttpGet(url);
	 		try {
	 			HttpResponse response = client.execute(request);
	 			final int status = response.getStatusLine().getStatusCode();
	 			if(status != HttpStatus.SC_OK) {
	 				toast("ERROR CODE: " + status + " for URL " + url);
	 				return null;
	 			}
	 			HttpEntity responseEntity = response.getEntity();
	 			return responseEntity.getContent();
	 		}
	 		catch(IOException e) {
	 			request.abort();
	 			toast("ERROR for " + url);
	 		}
	 		return null;
	 	}
	}
	
	public void toast(String s) {
		CharSequence text = s;
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(getActivity(), text, duration);
		toast.show();
	}
	
}
